package com.easy.base.api.model.dto;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResultDTO<T> {

    ResultDTO(HttpStatus status) {
        this.status = status;
        this.message = "";
        this.obj = null;
        this.count = 0;
    }

    ResultDTO(HttpStatus status, String message) {
        this.status = status;
        this.message = message;
        this.obj = null;
        this.count = 0;
    }

    ResultDTO(HttpStatus status, T obj) {
        this.status = status;
        this.message = "";
        this.obj = obj;
        this.count = 0;
    }

    ResultDTO(HttpStatus status, T obj, String message) {
        this.status = status;
        this.message = message;
        this.obj = obj;
        this.count = 0;
    }

    ResultDTO(HttpStatus status, T obj, int count) {
        this.status = status;
        this.message = "";
        this.obj = obj;
        this.count = count;
    }

    public static <T> ResultDTO<T> CreateResult(HttpStatus status) {
        return new ResultDTO<>(status);
    }

    public static <T> ResultDTO<T> CreateResult(HttpStatus status, String message) {
        return new ResultDTO<>(status, message);
    }

    public static <T> ResultDTO<T> CreateResult(HttpStatus status, T obj, int count) {
        return new ResultDTO<>(status, obj, count);
    }

    public static <T> ResultDTO<T> CreateResult(HttpStatus status, T obj) {
        return new ResultDTO<>(status, obj);
    }

    public ResponseEntity<?> CreateResponse() {
        if (this.getStatus().is2xxSuccessful()) {
            return new ResponseEntity<>(
                    this.getObj() == null ? this.getMessage() : this.getObj(),
                    this.getStatus());
        } else {
            return new ResponseEntity<>(this.getMessage(), this.getStatus());
        }
    }

    /**
     * 处理结果
     */
    private HttpStatus status;

    /**
     * 处理结果信息
     */
    private String message;

    /**
     * 处理结果对象
     */
    private T obj;

    /**
     * 处理结果列表总数
     */
    private int count;
}
